//
//  ConstellationDataBase.m
//  Today
//
//  Created by dllo on 15/5/28.
//  Copyright (c) 2015年 fanfan. All rights reserved.
//

#import "ConstellationDataBase.h"
static sqlite3 *dbconstellation = nil;
@implementation ConstellationDataBase

+ (void)openDataBase{
    if (dbconstellation) {
        return;
    }
    NSLog(@"%@", NSHomeDirectory());
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    path = [path stringByAppendingPathComponent:@"constellations.rdb"];
    sqlite3_open(path.UTF8String, &dbconstellation);
}

+ (void)createTable{
    [self openDataBase];
    NSString *sql = @"create table constellation (name varchar(20), constellationSource varchar(5000), constellationLove varchar(5000), constellationCharacter varchar(5000))";
    sqlite3_exec(dbconstellation, sql.UTF8String, NULL, NULL, NULL);
}

+ (void)insertValue:(NSString *)namestr :(NSString *)sourcestr :(NSString *)lovestr :(NSString *)characterstr{
    [self openDataBase];
    NSArray *array = [NSArray array];
    array = [ConstellationDataBase selectCharactData];
    if (array.count == 0) {
        NSString *sql = [NSString stringWithFormat:@"insert into constellation values ('%@', '%@', '%@', '%@')", namestr, sourcestr, lovestr, characterstr];
        sqlite3_exec(dbconstellation, sql.UTF8String, NULL, NULL, NULL);
    } else{
        return;
    }
    
}
+ (NSArray *)selectSourceData{
    [self openDataBase];
    sqlite3_stmt *stmt = nil;
    NSString *sql = @"select * from constellation";
    int result = sqlite3_prepare_v2(dbconstellation, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        NSMutableArray *array = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *constellationSource = sqlite3_column_text(stmt, 1);
            NSString *string = [NSString stringWithUTF8String:(const char *) constellationSource];
            [array addObject:string];
        }
        sqlite3_finalize(stmt);
        return array;
    }
    sqlite3_finalize(stmt);
    return [NSMutableArray array];
}

+ (NSArray *)selectloveData{
    [self openDataBase];
    sqlite3_stmt *stmt = nil;
    NSString *sql = @"select * from constellation";
    int result = sqlite3_prepare_v2(dbconstellation, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        NSMutableArray *array = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *constellationlove = sqlite3_column_text(stmt, 2);
            NSString *string = [NSString stringWithUTF8String:(const char *) constellationlove];
            [array addObject:string];
        }
        sqlite3_finalize(stmt);
        return  array;
    }
    sqlite3_finalize(stmt);
    return [NSMutableArray array];
}

+ (NSArray *)selectCharactData{
    [self openDataBase];
    sqlite3_stmt *stmt = nil;
    NSString *sql = @"select * from constellation";
    int result = sqlite3_prepare_v2(dbconstellation, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        NSMutableArray *array = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *constellationcharacter = sqlite3_column_text(stmt, 3);
            NSString *string = [NSString stringWithUTF8String:(const char *) constellationcharacter];
            [array addObject:string];
        }
        sqlite3_finalize(stmt);
        return array;
    }
    sqlite3_finalize(stmt);
    return [NSMutableArray array];
}

@end
